//
// Created by uaniheng on 2020/10/18.
//
#include "../../common.h"

/**
 * Definition for singly-linked list.
 *
**/

class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n) {

        auto v = new ListNode();
        v->next = head;
        ListNode *fast = head, *slow = v;
        int step = 0;
        while (fast->next) {
            fast = fast->next;
            if (++step >= n) {
                slow = slow->next;
            }
        }
        auto temp = slow->next;
        slow->next = slow->next->next;
        delete temp;
        return v->next;
    }
};
